#! /usr/bin/env python
"""Run all pluster unit tests."""

import cStringIO
import logging
import os
import sys
import unittest

sys.path.append('.')
import configtest
import localEnvtest
import logtest
import rpctest
import rrdtest
import tasktest


class Loader(unittest.TestLoader):
    """Filters out expensive or hard-to-run test cases."""
    def loadMostTests(self, module, skip):
        """Return a suite of a module's test cases, skipping named tests."""
        names = self.getTestCaseNames(module)
        suites = [self.loadTestsFromName(name, module)
                  for name in names if not name in skip]
        return self.suiteClass(suites)


def all_tests():
    """Scans a lexically ordered list of pluster modules to find all tests."""

    # Avoid one email test, as configuring the client host may be challenging.
    suite = Loader().loadMostTests(logtest.TestLog, ['test_email_log'])

    # Add all remaining pluster tests.
    for module in [#tasktest,   # Skip these X11 tests; they are expensive.
                   configtest,
                   localEnvtest,
                   rpctest,
                   rrdtest]:

        suite.addTest(Loader().loadTestsFromModule(module))

    return suite

if __name__ == '__main__':
    if os.path.isdir('MVC') and os.path.isdir('test'):
        os.chdir('test')   # This way ``python test/all.py'' will succeed, too.
    out = cStringIO.StringIO()
    logging.basicConfig(stream=out)
    results = unittest.TextTestRunner(verbosity=2).run(all_tests())
    if not results.wasSuccessful():
        for failure in results.failures:
            print "FAIL:", failure[0]
        for error in results.errors:
            print "ERROR:", error[0]
